require('module-alias/register'); //配置@路径
const path = require('path');
const express = require('express');
const cookieParser = require('cookie-parser');
const app = express();
const router = express.Router();

// 将环境变量从 .env 文件加载到 process.env 中
require('dotenv').config({
  override: true,
  path: path.join(process.cwd(), `.env.${process.env.NODE_ENV}`)
});
const interceptor = require('@/src/middlewares/interceptor');
const resAPI = require('@/src/middlewares/resAPI');
require('./utils/swaggerUI')(app);

console.log(process.env.ApiSign);
const PORT = process.env.PORT

// 设置静态资源地址
app.use('/public', express.static('public'));
app.use('/docs', express.static('docs'));
app.use(cookieParser());
app.use(express.urlencoded({ extended: false }));
app.use(express.json());
const arrRoutes = require('./routers');

app.get('/', (req, res) => res.send('访问成功'));

app.use(function (req, res, next) {
  res.header('Access-Control-Allow-Origin', '*');
  res.header('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept');
  next();
});

// app.use的使用顺序需要注意

// 接口返回封装
app.use(resAPI);
// 请求拦截处理
app.use(interceptor);
for (const route of arrRoutes) {
  app.use(route.path, route.component);
}
app.use(router);
app.listen(PORT, () => {
  console.log(`Web server started at port ${PORT}!`);
});
module.exports = app;
